//index.js
const config = require('../../config.js')
const http = require('../../http.js')
//获取应用实例
const app = getApp()
Page({
  data: {
    conferenceData: [],
    bannerData: ["https://cdn.pluslegal.cn/civil_home_banner@2x.png"],
    civilbanner:["https://cdn.pluslegal.cn/home_img_civil@2x.png"],
    rule: "",
    showModal: false,
    swiperCurrent: 0,
    roomName: '',
    downloadImgSrc:'',
  },

  onLoad: function (options) {
    //this.getConferences();   
    this.getcalendar();//获取日历
    this.getBanner();//获取banner
  },

  onClick: function (e) {
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: '../login/login',
      })
      return
    }
    let type = parseInt(e.currentTarget.dataset.type)
    switch (type) {
      //每日一典
      case 1:
        this.showHint();
        break;
        //公开课
      case 2:
        wx.navigateTo({
          url: "../civilclass/civilclass",
        })
        break
      case 3:
        wx.navigateTo({
          url: "../contact/contact",
        })
        break
      case 4:
        wx.navigateTo({
          url: "../problem/problem",
        })
        break
      case 5:
        wx.navigateTo({
          url: "../mine/mine",
        })
        break
      case 6:
        //关闭弹窗
        this.hideModal()
        break
      case 7:
        //下载到本地
       this.downloadimg();
        break
        case 8:
          wx.navigateTo({
            url: "../read/read",
          })
          break
          case 9:
          this.addranking('civilcode')
          wx.navigateTo({
            url: "../civil/civil",
          })
          break;
        case 10:
        this.addranking('game')
        wx.navigateToMiniProgram({
          appId: 'wx675db00d4f02fa1a',
          envVersion: 'release',
          success: (res) => {
              console.log('--此接口调用成功后的返回信息--')
          }
      })
        break;
        case 11:
        wx.navigateTo({
          url: "../quicklyAsk/quicklyAsk",
        })
        break;
        case 12:
          wx.navigateTo({
            url: "../civilstar/civilstar",
          })
          break;     
    }
  },
  addranking: function (modular) {
    var that = this
    http.request(
      config.addranking+"/"+ modular,
      'POST', null,
      function (res) {
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  },
  getcalendar:function(){
    var that = this
    http.request(
      config.calendar,
      'GET',null,
      function (res) {
        if(res.data != null && res.data !=""){
          that.setData({
            downloadImgSrc: res.data.calendarResources,
          })
        }else{
          wx.showToast({
            title: "获取数据失败！",
            icon: 'none',
          })
        }
       
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }, true
    )

  },

  showHint: function () {
    this.setData({
      showModal: true,
      roomName: '',
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {},
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },

  swiperChange: function (e) {
    console.log("swiperChange", e)
    let cur = e.detail.current
    this.setData({
      swiperCurrent: cur,
      rule: this.data.conferenceData[cur].content
    })
  },

  downloadimg: function() {
    var that = this
    // 通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
    wx.getSetting({
      success(res) {
        if (!res['scope.writePhotosAlbum']) {
          wx.authorize({ 
            scope: 'scope.writePhotosAlbum', 
            success() {
              wx.downloadFile({ 
                url:that.data.downloadImgSrc,
                success: function(res) {
                  wx.saveImageToPhotosAlbum({
                    filePath:res.tempFilePath,
                    success(res) {
                      console.log(res);
                      wx.showToast({
                        title: "保存成功",
                        duration: 2000 
                      }) 
                      that.hideModal()
                    }  
                  }) 
                }  
              })
            }
          })
        }else {
          console.log('error');
        }  
      }  
    }) 
  },

//获取banner
getBanner: function (id, name) {
  var that = this
  http.request(
    config.getbanner+'/'+1,
    'GET',null,
    function (res) {
      if(res.data != null && res.data !=""){
        that.setData({
          bannerData: res.data
        })
      }else{
        wx.showToast({
          title: '暂无数据！',
          icon: 'none',
        })
      }
    },
    function (err) {
      wx.showToast({
        title: err,
        icon: 'none',
      })
    }, true
  )
},

toDetails:function(e){
  let NextUrl = e.currentTarget.dataset.src;
  if(NextUrl=="openClass"){
    wx.navigateTo({
      url: "../civilclass/civilclass",
    })
  }else{
    wx.navigateTo({
      url: "../outlink/outlink?url="+NextUrl,
    })
  }
},
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log("onShareAppMessage", res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/pages/index/index'
    }
  }

})